package com.gxa.dao.impl;

import com.gxa.common.Constants;
import com.gxa.dao.CustomerDao;
import com.gxa.dbutils.JdbcBuild;
import com.gxa.pojo.CareRemind;
import com.gxa.pojo.Customer;
import com.gxa.pojo.Orders;

import java.sql.SQLException;
import java.util.List;

public class CustomerDaoImpl extends JdbcBuild implements CustomerDao {
    @Override
    public List queryCustomer() throws SQLException {
        String sql = "select * from customer where ask_time is null";
        Object[] params = null;
        List<Customer> list = super.queryListEntity(Customer.class, sql, params);
        return list;
    }

    /**
     * 客户资源管理：查询客户
     * @param currentPage
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List queryCustomer(Integer currentPage, Integer pageSize) throws SQLException {
        String sql = "select * from customer where del_status=? limit ?,?";
        Object[] params = {Constants.STATUS_OK,currentPage, pageSize};
        List<Customer> list = super.queryListEntity(Customer.class, sql, params);
        return list;
    }

    /**
     * 客户资源管理：查询客户总数
     * @return
     */
    @Override
    public Integer countCustomer() throws SQLException {
        String sql = "select count(*) from customer";
        int count = super.findAllForInt(sql, null);
        return count;
    }

    /**
     * 客户满意度管理：按照客户名查询客户满意度
     * @param currentPage
     * @param pageSize
     * @param customer_name
     * @return
     * @throws SQLException
     */
    @Override
    public List searchCustomer(Integer currentPage, Integer pageSize, String customer_name) throws SQLException {
        List<Customer> list = null;
        if(customer_name!=null){
            String sql = "select * from customer where del_status=? and customer_name=? and ask_time is not null limit ?,? ";
            Object[] params = {Constants.STATUS_OK,customer_name,currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }else if(customer_name==null){
            String sql = "select * from customer where del_status=? and ask_time is not null limit ?,? ";
            Object[] params = {Constants.STATUS_OK,currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }
        return list;
    }

    /**
     * 查询指定客户的所有订单信息
     * @param customer_name
     * @return
     * @throws SQLException
     */
    @Override
    public List queryOrder(String customer_name) throws SQLException {
        String sql = "select * from orders where name=?";
        Object[] params = {customer_name};
        List<Orders> list = super.queryListEntity(Orders.class, sql, params);
        return list;
    }
    @Override
    public List searchCustomer(Integer currentPage, Integer pageSize,String customer_name,Long mobile_phone) throws SQLException {
        List<Customer> list = null;
        if(customer_name!=null&&mobile_phone!=0){
//            System.out.println("customer_name!=null&&mobile_phone!=0");
            String sql = "select * from customer where del_status=? and customer_name=? and mobile_phone=? limit ?,? ";
            Object[] params = {Constants.STATUS_OK,customer_name,mobile_phone,currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }else if(customer_name==null&&mobile_phone!=0){
//            System.out.println("customer_name==null&&mobile_phone!=0");
            String sql = "select * from customer where del_status=? and mobile_phone=? limit ?,? ";
            Object[] params = {Constants.STATUS_OK,mobile_phone,currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }else if(customer_name!=null&&mobile_phone==0){
//            System.out.println("customer_name!=null&&mobile_phone==0");
            String sql = "select * from customer where del_status=? and customer_name=? limit ?,? ";
            Object[] params = {Constants.STATUS_OK,customer_name,currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }else if(customer_name==null&&mobile_phone==0){
//            System.out.println("customer_name==null&&mobile_phone==0");
            String sql = "select * from customer where del_status=? limit ?,? ";
            Object[] params = {Constants.STATUS_OK,currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }
        return list;
    }

    @Override
    public Integer insertCustomer(Customer customer) throws SQLException {
        String sql = "insert into customer(customer_code,customer_name,sex,birth,type,status,mobile_phone,office_phone,email,home_phone,address,remark,entry_time,sale_admin,del_status) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] params = {customer.getCustomer_code(),customer.getCustomer_name(),customer.getSex(),customer.getBirth(),customer.getType(),customer.getStatus(),customer.getMobile_phone(),customer.getOffice_phone(),customer.getEmail(),customer.getHome_phone(),customer.getAddress(),customer.getRemark(),customer.getEntry_time(),customer.getSale_admin(),Constants.STATUS_OK};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public Integer deleteCustomer(Integer id) throws SQLException {
        String sql = "update customer set del_status=? where id=?";
        Object[] params = {Constants.STATUS_DEL,id};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public Integer updateCustomer(Customer customer) throws SQLException {
        String sql = "update customer set customer_code=?,customer_name=?,sex=?,birth=?,type=?,status=?,mobile_phone=?,office_phone=?,email=?,home_phone=?,address=?,remark=?,entry_time=?,sale_admin=? where id=?";
        Object[] params = {customer.getCustomer_code(),customer.getCustomer_name(),customer.getSex(),customer.getBirth(),customer.getType(),customer.getStatus(),customer.getMobile_phone(),customer.getOffice_phone(),customer.getEmail(),customer.getHome_phone(),customer.getAddress(),customer.getRemark(),customer.getEntry_time(),customer.getSale_admin(),customer.getId()};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public List queryCustomerSatisfaction(Integer currentPage, Integer pageSize) throws SQLException {
        String sql = "select * from customer where del_status=? and ask_time is not null limit ?,?";
        Object[] params = {Constants.STATUS_OK,currentPage, pageSize};
        List<Customer> list = super.queryListEntity(Customer.class, sql, params);
        return list;
    }

    @Override
    public Integer addCustomerSatisfaction(Customer customer) throws SQLException {
        String sql = "update customer set quality_satisfaction=?,service_satisfaction=?,cost_satisfaction=?,total_satisfaction=?,customer_feedback=?,ask_time=? where id=?";
        Object[] params = {customer.getQuality_satisfaction(),customer.getService_satisfaction(),customer.getCost_satisfaction(),customer.getTotal_satisfaction(),customer.getCustomer_feedback(),customer.getAsk_time(),customer.getId()};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public Integer updateCustomerSatisfaction(Customer customer) throws SQLException {
        String sql = "update customer set quality_satisfaction=?,service_satisfaction=?,cost_satisfaction=?,total_satisfaction=?,customer_feedback=? where id=?";
        Object[] params = {customer.getQuality_satisfaction(),customer.getService_satisfaction(),customer.getCost_satisfaction(),customer.getTotal_satisfaction(),customer.getCustomer_feedback(),customer.getId()};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public List queryCustomerCareRemind(Long customer_code, Integer currentPage, Integer pageSize) throws SQLException {
        String sql = "select * from customer_remind where customer_code=? and del_status=? limit ?,?";
        Object[] params = {customer_code,Constants.STATUS_OK,currentPage,pageSize};
        List<CareRemind> list = super.queryListEntity(CareRemind.class, sql, params);
        return list;
    }

    @Override
    public Integer addCustomerCareRemind(CareRemind careRemind) throws SQLException {
        String sql = "insert into customer_remind(customer_code,customer_name,remind_name,remind_time,remind_type,remind_cycle,remind_rate,remark,status,del_status) values(?,?,?,?,?,?,?,?,?,?)";
        Object[] params = {careRemind.getCustomer_code(),careRemind.getCustomer_name(),careRemind.getRemind_name(),careRemind.getRemind_time(),careRemind.getRemind_type(),careRemind.getRemind_cycle(),careRemind.getRemind_rate(),careRemind.getRemark(),Constants.STATUS_OK,Constants.STATUS_ENABLE};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public Integer updateCustomerCareRemind(CareRemind careRemind) throws SQLException {
        String sql = "update customer_remind set customer_code=?,customer_name=?,remind_name=?,remind_time=?,remind_type=?,remind_cycle=?,remind_rate=?,remark=? where customer_code=? and customer_name=?";
        Object[] params = {careRemind.getRemind_name(),careRemind.getRemind_time(),careRemind.getRemind_type(),careRemind.getRemind_cycle(),careRemind.getRemind_rate(),careRemind.getRemark(),careRemind.getCustomer_code(),careRemind.getCustomer_name()};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public Integer deleteCustomerCareRemind(Integer id) throws SQLException {
        String sql = "update customer_remind set del_status=? where id=?";
        Object[] params = {Constants.STATUS_DEL,id};
        int result = super.executeUpdate(sql, params);
        return result;
    }

    @Override
    public List searchCustomerCareRemind(Integer currentPage, Integer pageSize, String customer_name) throws SQLException {
        List<Customer> list = null;
        if(customer_name!=null){
            String sql = "select * from customer where customer_name=? and del_status=? limit ?,?";
            Object[] params = {customer_name,Constants.STATUS_OK,currentPage,pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }else if(customer_name==null){
            String sql = "select * from customer where del_status=? limit ?,?";
            Object[] params = {Constants.STATUS_OK,currentPage,pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }
        return list;
    }

}
